package cn.wwcyc.controller;

import cn.wwcyc.domain.R;
import cn.wwcyc.domain.TjDw;
import cn.wwcyc.domain.TjGx;
import cn.wwcyc.domain.TjLog;
import cn.wwcyc.mapper.TjGxMapper;
import cn.wwcyc.service.TjDwService;
import cn.wwcyc.service.TjGxService;
import cn.wwcyc.service.TjLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/log")
public class LOGController {
    @Autowired
    TjGxService tjGxService;
    @Autowired
    TjGxMapper tjGxMapper;
    @Autowired
    TjDwService tjDwService;

    @Autowired
    TjLogService tjLogService;

    @GetMapping("/getpage")
    public R<IPage<TjLog>> getPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end,
            @RequestParam(required = false) String jh) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 15;
        }
        LambdaQueryWrapper<TjLog> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(jh)) {
            qw.like(TjLog::getOpcontent, jh);
        }
        if (end == null) {
            end = new Date();
        }
        if (start == null) {
            start = new Date();
        }
        qw.between(TjLog::getCreatedate, start, end);
        qw.orderByDesc(TjLog::getCreatedate);
        IPage<TjLog> page = new Page<>(pageNum, pageSize);
        tjLogService.page(page, qw);
        return R.success(page);
    }

    //根据单位id查找干线,次函数获取的是本单位下所有下属单位及本身的所拥有的干线，与业务不符合！！
    // 下面面的getListByDw1是对他的修正
    @GetMapping("getlistbydw")
    public R<List<TjGx>> getListByDw(@RequestParam(required = false) Long dwid) {

        if (dwid == null) {
            return R.success(null);
        }
        TjDw tjDw = tjDwService.getDwTreeById(dwid);
        List<TjDw> dwList = new ArrayList<>();
        dwList = tjDwService.DwTreeToList(dwList, tjDw);

        List<Long> longList = new ArrayList<>();
        for (TjDw dw : dwList) {
            longList.add(dw.getId());
        }
        LambdaQueryWrapper<TjGx> qw = new LambdaQueryWrapper<>();
        qw.in(TjGx::getDw, longList);
        List<TjGx> gxList = tjGxMapper.selectList(qw);
        return R.success(gxList);
    }

    //根据单位获取干线，获取的是路径上所有单位的干线,及本单位下所有下属单位及本身的所拥有的干线
    @GetMapping("getlistbydw1")
    public R<List<TjGx>> getListByDw1(@RequestParam(required = false) Long dwid) {

        if (dwid == null) {
            return R.success(null);
        }
        List<Long> dwIdList = tjDwService.getPathByid(dwid);

        TjDw tjDw = tjDwService.getDwTreeById(dwid);
        List<TjDw> dwList = new ArrayList<>();
        dwList = tjDwService.DwTreeToList(dwList, tjDw);
        List<Long> longList = new ArrayList<>();
        for (TjDw dw : dwList) {
            longList.add(dw.getId());
        }

        dwIdList.addAll(longList);

        LambdaQueryWrapper<TjGx> qw = new LambdaQueryWrapper<>();
        qw.in(TjGx::getDw, dwIdList);
        List<TjGx> gxList = tjGxMapper.selectList(qw);
        return R.success(gxList);
    }

    //获取所有干线
    @GetMapping("getAll")
    public R<List<TjGx>> getAll() {
//        System.out.println("dwid:" + dwid);

//
//        LambdaQueryWrapper<TjGx> qw = new LambdaQueryWrapper<>();
//        qw.in(TjGx::getDw, longList);
        List<TjGx> gxList = tjGxMapper.selectList(null);
        return R.success(gxList);
    }


    @GetMapping("getById")
    public R<TjGx> getById(@RequestParam Long id) {

        TjGx gx = tjGxMapper.selectById(id);
        return R.success(gx);
    }


    @PostMapping("update")
    public R<Integer> update(@RequestBody TjGx gx) {

        Integer i = tjGxMapper.updateById(gx);
        return R.success(i);
    }


    @PostMapping("save")
    public R<Integer> save(@RequestBody TjGx gx) {
        Integer i = tjGxMapper.insert(gx);
        return R.success(i);
    }

    @DeleteMapping
    public R del(Integer id) {
        tjGxService.removeById(id);
        return R.success();
    }

    @PostMapping("delbatch")
    public R delbatch(@RequestBody List<TjGx> tjGxList) {
        for (TjGx gx : tjGxList) {
            Long id = gx.getId();
            tjGxService.removeById(id);
        }
        return R.success();
    }



}
